import { Component, OnInit } from '@angular/core'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { FollowService } from '../services/follow.service'

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class IndexComponent implements OnInit {
  // tab
  tabs = ['楼盘', '户型', '活动']
  active = 0
  // 楼盘户型活动
  estate = []
  layout = []
  activity = []
  estateNomore = false
  layoutNomore = false
  activityNomore = false
  error = false
  loading = false
  // 是否第一次初始化(第一次加载失败不显示error组件)
  initBool = false

  constructor(private utils: CommonUtil, private service: FollowService) {
    // 触底翻页
    window.onscroll = e => {
      let clientHeight = document.documentElement.clientHeight
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      let scrollHeight = document.body.scrollHeight
      let key = this.getAliasByActive()
      // 触底了
      if (scrollTop + clientHeight === scrollHeight) {
        setTimeout(() => {
          if (!this[key + 'Nomore'] && !this.error) {
            this.getList(this.service[key].pager.Start)
          }
        }, 1000)
      }
    }
  }

  // 初始化
  ngOnInit() {
    this.initBool = true
    this.loading = true
    this.getList(0, 'estate')
    this.getList(0, 'layout')
    this.getList(0, 'activity')
  }
  // 切换tab
  handleTabChange(e) {
    let key = this.getAliasByActive()
    document.body.scrollTop = document.documentElement.scrollTop = 0
    this.active = e.index
    this.getList(0, key)
  }
  // 获取列表(start: 起始条目，KEY?：列表名称)
  getList(start, KEY?) {
    let key = KEY || this.getAliasByActive()
    let funcName = `get${this.upperFirstLetter(key)}List`
    this.service[funcName](
      start,
      res => {
        setTimeout(() => {
          this.error = false
          this.loading = false
        }, 500)
        if (res.Success && res.Result) {
          // 下一次的起始条目需要加上本次请求到的数据条数
          this.service[key].pager.Start += res.Result.Value.length
          // 如果本次请求到的数据小于每页总数，说明没有下一页了
          if (res.Result.Value.length < this.service[key].pager.Limit) {
            this[key + 'Nomore'] = true
          } else {
            this[key + 'Nomore'] = false
          }
          // 数据处理
          let data = res.Result.Value.map(item => {
            let obj = this[key + 'Filter'](item)
            return obj
          })
          // 如果是第一页，那先把数据清空
          if (start === 0) {
            this[key] = []
          }
          this[key] = this[key].concat(data)
        } else {
          this[key + 'Nomore'] = false
        }
      },
      err => {
        this.loading = false
        if (!this.initBool) {
          this.error = true
        }
      }
    )
  }
  // list列表优化
  trackByFn(index, item) {
    return item.Id
  }

  // 根据当前激活的tab获取当前所在模块
  getAliasByActive() {
    let active = this.active
    if (active === 0) {
      return 'estate'
    } else if (active === 1) {
      return 'layout'
    } else {
      return 'activity'
    }
  }
  // 楼盘数据过滤
  estateFilter(item) {
    let Photo = this.utils.getThumbnail(item.Photo)
    let Tag = this.utils.getTag(item.Tag)
    item.Photo = Photo
    item.Tag = Tag
    if (item.Landmark) {
      item.Landmark = JSON.parse(item.Landmark)[0]
    }
    return item
  }
  // 户型数据过滤
  layoutFilter(item) {
    let Photo = this.utils.getThumbnail(item.Photo)
    item.Photo = Photo
    return item
  }
  // 活动数据过滤
  activityFilter(item) {
    let Desc = JSON.parse(item.Description)
    let res = ''
    for (let i = 0; i < Desc.length; i++) {
      if (Desc[i].Code === 'promotionDetail') {
        res = Desc[i].Value
      }
    }
    item.Description = res
    return item
  }
  // 重新加载
  handleReload() {
    let key = this.getAliasByActive()
    this.error = false
    this.loading = true
    this.getList(this.service[key].pager.Start)
  }
  // 首字母大写
  upperFirstLetter(str) {
    if (str.length === 0) {
      return ''
    } else {
      let first_l = str.substring(0, 1)
      let first_u = first_l.toUpperCase()
      return first_u + str.substring(1, str.length)
    }
  }
}
